'use strict';

/* Services */
myApp.factory("commonService", function($q, $http, $rootScope) { 
    
    	var context = 'service/ws/';
    	
    	var deferred = $q.defer();
	
	function excuteInvoke(url, params ,method){

	    $http({
		method : method,
		url : context+url ,
		params: params
	    }).
    	    success(function(data, status, headers, config) {
//    		console.log('onSuccess');
//    		console.log(data);
    		deferred.resolve(data);
    	    }).
    	    error(function(data, status, headers, config) {
//    		console.log('onFail');
    		deferred.resolve(result);
	    });
//	    deferred.resolve(result);
	    
	    return deferred.promise;
	    
	}
	
	return { 
    		get : function(url, params) { 
    		    return excuteInvoke(url, params ,'GET');
            },
    		post : function(url, params) { 
    		    return excuteInvoke(url, params , 'POST');
            }
    	};
});


myApp.service("ajaxService", function() {
	this.obj = {};
	
	return { 
//	    		get : function(url, params) {
//	    return excuteInvoke(url, params, 'GET');
//	},
	post : function(url, params) {
	    var context = 'service/ws/';
	    $.post(
		    context+url, 
		    params
	    , function(data) {
//		console.log(data.name); // John
//		console.log(data.time); // 2pm
//			_log(data);
			return data;
	    }, "json");
	}
    };
});


function debug(msg)
{
	console.log(msg);
}

function NVL(v)
{
	return v==null || v==undefined?'':v;
}

function displayJSON(jsonObject)
{
	debug("---------------- displayJSON() -------------");
	$.each(jsonObject, function(key, value) {
	    debug(key+': '+value);
	});
	debug("--------------------------------------------");
}

function resetJSON(jsonObject,defaultValue)
{
	debug("resetJSON()");
	$.each(jsonObject, function(key, value) {
	    eval("jsonObject."+key+"="+defaultValue+";");
	});
}
